package boot

import (
	"fmt"
	"yunj/app/es/config"
	"yunj/pkg/global"

	"github.com/olivere/elastic/v7"
)

// 初始化ES连接
func InitEs() {
	if !global.Config.Elasticsearch.Enable {
		return
	}
	client, err := elastic.NewClient(
		elastic.SetURL(global.Config.Elasticsearch.Hosts...),
		elastic.SetSniff(false), // 禁用 sniffing
	)
	if err != nil {
		panic(fmt.Sprintf("初始化ES连接失败！%v", err))
	}
	global.EsClient = client
	// 注册全部索引
	for _, index := range config.Indexs {
		if err = index.IndexRegister(); err != nil {
			panic(fmt.Sprintf("ES索引%s注册失败！%v", index.FullName(), err))
		}
	}
}
